import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/station',
    // redirect: '/dashboard',
    hidden: true,
    children: [
      {
        path: 'dashboard',
        redirect: '/station',
        name: 'Dashboard',
        meta: { title: '总览', icon: 'dashboard', affix: true }
      }
      // {
      //   path: 'dashboard',
      //   component: () => import('@/views/dashboard/home'),
      //   name: 'Dashboard',
      //   meta: { title: '总览', icon: 'dashboard', affix: true }
      // }
    ]
  },
  {
    path: '/station',
    component: Layout,
    redirect: '/station/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/station/index'),
        name: 'Station',
        meta: { title: '场站管理', icon: 'tree' }
      },
      {
        hidden: true,
        path: 'section/:stationId',
        component: () => import('@/views/section/index'),
        name: 'Section',
        meta: { title: '区域', icon: 'component', noCache: true }
      },
      {
        hidden: true,
        path: 'place',
        component: () => import('@/views/place/index'),
        name: 'Place',
        meta: { title: '车位', icon: 'component', noCache: true }
      },
      {
        hidden: true,
        path: 'detail/:stationId',
        component: () => import('@/views/station/detail'),
        name: 'Detail',
        meta: { title: '场站详情', icon: 'component', noCache: true }
      }
    ]
  },
  {
    path: '/priceedit',
    component: Layout,
    redirect: '/priceedit/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/priceedit/index'),
        name: 'PriceEdit',
        meta: { title: '价格修改', icon: 'guide', noCache: true }
      }
    ]
  },
  {
    path: '/etemplate',
    component: Layout,
    redirect: '/etemplate/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/etemplate/index'),
        name: 'Price',
        meta: { title: '模板管理', icon: 'example', noCache: true }
      }
    ]
  },
  {
    path: '/pricelog',
    component: Layout,
    redirect: '/pricelog/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/pricelog/index'),
        name: 'PriceLog',
        meta: { title: '日志记录', icon: 'chart', noCache: true, badage: 'unreadCount' }
      }
    ]
  },
  {
    path: '/errorlog',
    component: Layout,
    redirect: '/errorlog/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/errorlog/index'),
        name: 'ErrorLog',
        meta: { title: '异常记录', icon: 'chart', noCache: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人信息', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/workorder',
    component: Layout,
    redirect: '/workorder/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/WorkOrder/index'),
        name: 'Profile',
        meta: { title: '工单', icon: 'user', noCache: true }
      }
    ]
  },
  // {
  //   path: '/conference',
  //   component: Layout,
  //   redirect: '/conference/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/conference/index'),
  //       name: 'conference',
  //       meta: { title: '会议门牌', icon: 'user', noCache: true }
  //     }
  //   ]
  // }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/etag',
    component: Layout,
    redirect: '/etag/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/etag/index'),
        name: 'Etag',
        meta: { title: '设备清单', icon: 'guide', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/price',
    component: Layout,
    redirect: '/price/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/price/index'),
        name: 'Price',
        meta: { title: '价格管理', icon: 'money', noCache: true, roles: ['superadmin'] }
      }
    ]
  },
  {
    path: '/actionlogs',
    component: Layout,
    redirect: '/actionlogs/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/guide/index'),
        name: 'Guide',
        meta: { title: '操作记录', icon: 'documentation', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/users',
    component: Layout,
    redirect: '/users/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/users/index'),
        name: 'Users',
        meta: { title: '用户管理', icon: 'peoples', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    redirect: '/settings/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/settings/index'),
        name: 'Setting',
        meta: { title: '系统设置', icon: 'list', noCache: true, roles: ['admin'] }
      }
    ]
  },
  // {
  //   path: '/permission',
  //   component: Layout,
  //   redirect: '/permission/page',
  //   alwaysShow: true, // will always show the root menu
  //   hidden: true,
  //   name: 'Permission',
  //   meta: {
  //     title: '权限设置',
  //     icon: 'lock',
  //     roles: ['admin', 'editor'] // you can set roles in root nav
  //   },
  //   children: [
  //     {
  //       path: 'page',
  //       component: () => import('@/views/permission/page'),
  //       name: 'PagePermission',
  //       meta: {
  //         title: '权限示例',
  //         roles: ['admin'] // or you can only set roles in sub nav
  //       }
  //     },
  //     {
  //       path: 'directive',
  //       component: () => import('@/views/permission/directive'),
  //       name: 'DirectivePermission',
  //       meta: {
  //         title: '切换角色'
  //         // if do not set roles, means: this page does not require permission
  //       }
  //     },
  //     {
  //       path: 'role',
  //       component: () => import('@/views/permission/role'),
  //       name: 'RolePermission',
  //       meta: {
  //         title: '角色权限',
  //         roles: ['admin']
  //       }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
